package com.demos.cqp.dnt.util;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.demos.cqp.dnt.domain.DVD;

/**
 * Servlet implementation class CompteDVDServlet
 */
@WebServlet("/CompteDVDServlet")
public class CompteDVDServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CompteDVDServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String realisateur = (String)request.getParameter("real");
		System.out.println("test: " + realisateur);
		@SuppressWarnings("unchecked")
		List<DVD> listeDVD = (List<DVD>) request.getSession().getAttribute(
				"listeDVD");
		int compteur = 0;
		for (DVD dvd: listeDVD){
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (dvd.getRealisateur().equals(realisateur)){
				compteur ++;
			}
		}
		response.setCharacterEncoding("UTF-8");
		response.getWriter().append("<div><label>Vous avez " + compteur + " DVD du même auteur dans la DVDthèque</label></div>");

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
